#ifndef _MEM_REC_H_
#define _MEM_REC_H_
#include "util.h"


#define MAX_REC_SIZE 10

struct tMemoryUsage
{
	void* ptr;
	size_t size;
	char file[256];
	char usage[32];
	int line;
	
	tMemoryUsage();
	tMemoryUsage(const tMemoryUsage& MU);
	tMemoryUsage & operator= (const tMemoryUsage & MU);
	tMemoryUsage(void* p, size_t s, const char* f, const char* u, int l);
};


struct tMemoryUsageElement : public tMemoryUsage
{
	tMemoryUsageElement * Next;
	tMemoryUsageElement(tMemoryUsage & MU);
	tMemoryUsageElement& operator= (const tMemoryUsage & MU);
	void* operator new (size_t size);
	void  operator delete(void * p);
};

class tMemoryRecords
{
	char m_OutFileName[256];
	tMemoryUsageElement * ListHead;
	tMemoryUsageElement * EmptyMemoryUsageEl[MAX_REC_SIZE];
	tMutex Mutex;
public:
	tMemoryRecords(const char * filename);
	~tMemoryRecords();
	void AddToLink(tMemoryUsageElement * pMU);
	tMemoryUsageElement * AssignToEmptyElement(const tMemoryUsage * MU);
	void AddRec(tMemoryUsage MU);
	tMemoryUsageElement * RemoveEleInList(void * ptr);
	void AddEmpty(tMemoryUsageElement * pEmpty);
	void RemRec(void * p);
	void DumpLeak(const char * filepath);
};

void DummyFunc();

#ifdef _DEF_MEMORY_USAGE
tMemoryRecords gMemory(__FILE__);
#else
extern tMemoryRecords gMemory;
#endif

#ifdef _DEBUG_MEMORY_LEAK
void * operator new (size_t size, const char* file, int line, const char * usage);
void * operator new[] (size_t size, const char* file, int line, const char * usage);
void operator delete (void *p, const char* file, int line, const char * usage);
void operator delete[] (void *p, const char* file, int line, const char * usage);
void operator delete (void *p) throw();
void operator delete[] (void *p) throw();

#define DBG_NEW new(__FILE__, __LINE__, NULL)
#define DBG_ALLOC(f,l) new(f,l, NULL)
#else
#define DBG_NEW new
#define DBG_ALLOC(f,l) new
#endif

#endif
